//
//  ByLoginUserOperationView.m
//  beibei_ios
//
//  Created by mc on 16/4/26.
//  Copyright © 2016年 hebidu. All rights reserved.
//

#import "ByLoginUserOperationView.h"
#import "ByLoginCaptchaButton.h"
#import "ByTextField.h"
@interface ByLoginUserOperationView()<UITextFieldDelegate>
@property (nonatomic,assign)NSInteger line;             //行数
@property (nonatomic,assign)ByUserViewType type;        // 类型
@property (nonatomic,strong)NSArray *imgAry;            //图片数组
@property (nonatomic,strong)NSArray *titlesAry;         //标题数组

@property (nonatomic,strong)ByLoginCaptchaButton *captchaButton;

@end
@implementation ByLoginUserOperationView
- (instancetype)initWithFrame:(CGRect)frame Titles:(NSArray *)titleAry type:(ByUserViewType)type ImageAry:(NSArray *)imgArr{
    if (self = [super initWithFrame:frame]) {
        _titlesAry = titleAry;
        _type = type;
        _imgAry = imgArr;
        self.width = frame.size.width;
        self.height = frame.size.height;
        self.backgroundColor = [UIColor clearColor];
        [self createViews];
    }
    return self;
}
// 画线无图标
- (void)createViews{
    // logo
    CGFloat x,y,width,height;
    x = (SCREEN_WIDTH - 100)/2;
    y = 20;
    width = height = 100;
    UIImageView *logoImg = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    logoImg.layer.cornerRadius = height/2;
    logoImg.layer.masksToBounds = YES;
    logoImg.image = [UIImage imageNamed:@"Common_default_1_1"];
    [self addSubview:logoImg];
    
    // 循环创建textfiled
    ByTextField *filed ;
    for (int i = 0; i<_titlesAry.count; i++) {
        CGFloat margin; // 文本输入框之间的间距
        if (_type == ByUserViewLogin) {
            margin = 25;
        }else{
            margin = 20;
        }
        // 第一条线
        x = 20;
        y = CGRectGetMaxY(logoImg.frame) + LineHeight * (i + 1) + margin * i + (LineHeight - 8);
        width = 1;
        height = 8;
        UIView *lineView1 = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        lineView1.backgroundColor = BY_THEME_COLOR;
        [self addSubview:lineView1];
        
        // 输入框
        x = 25;
        y = CGRectGetMaxY(logoImg.frame) + 30 + LineHeight *i + margin*i;
        width = self.frame.size.width - x *2;
        filed = [[ByTextField alloc] initWithFrame:CGRectMake(x, y, width, LineHeight)];
        filed.backgroundColor = [UIColor clearColor];
        filed.borderStyle = UITextBorderStyleNone;
        filed.textColor = [UIColor whiteColor];
//        filed.delegate = self;
        // 若涉及输入密码
        if ([_titlesAry[i] containsString:@"密码"]) {
            filed.secureTextEntry = YES;
        }
        if ([_titlesAry[i] containsString:@"手机号"] || [_titlesAry[i] containsString:@"验证码"]) {
            filed.keyboardType = UIKeyboardTypeNumberPad;
        }
        NSMutableAttributedString *paleceholderAttr = [[NSMutableAttributedString alloc] initWithString:_titlesAry[i]];
        [paleceholderAttr addAttribute:NSFontAttributeName value:FONT(14) range:NSMakeRange(0, paleceholderAttr.length)];
        [paleceholderAttr addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, paleceholderAttr.length)];
        [filed setAttributedPlaceholder:paleceholderAttr];
        filed.tag = 10+ i;
        [self addSubview:filed];
        
        
        // 底部线条
        x = lineView1.right;
        y = CGRectGetMaxY(lineView1.frame) - 1;
        width = SCREEN_WIDTH - 2*20 - 2;
        height = 1;
        UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        lineView2.backgroundColor = BY_THEME_COLOR;
        [self addSubview:lineView2];
        
        // 右侧
        x = lineView2.right;
        y = CGRectGetMinY(lineView1.frame);
        width = 1;
        height = 8;
        UIView *lineView3 = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        lineView3.backgroundColor = BY_THEME_COLOR;
        [self addSubview:lineView3];
        
        // 若是含有密码
        if ([_titlesAry[i] containsString:@"密码"]) {
            width = filed.frame.size.width - 30;
            filed.size = CGSizeMake(width, LineHeight);
            x = 0;
            width = 30;
            height = LineHeight - 5;
            y = CGRectGetMinY(filed.frame) + 3;
            UIButton *passwordBtn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
            [self addSubview:passwordBtn];
            [passwordBtn setImage:[UIImage imageNamed:@"mine_pass"] forState:UIControlStateNormal];
            [passwordBtn setImage:[UIImage imageNamed:@"mine_passNO"] forState:UIControlStateSelected];
            passwordBtn.right = self.width - 30;
            [passwordBtn addTarget:self action:@selector(isShowOrNot:) forControlEvents:UIControlEventTouchUpInside];
            passwordBtn.tag = 100 + i;
            passwordBtn.selected = YES;
        }
    }
    // 若不是登录,第二栏都需要一个获得验证码的按钮
    if (_type != ByUserViewLogin) {
        filed = [self viewWithTag:11];
        width = filed.frame.size.width - 100;
        filed.size = CGSizeMake(width, LineHeight);
        x = 0;
        width = 90;
        height = LineHeight - 5;
        y = CGRectGetMinY(filed.frame) + 3;
        UIButton *getCodeBtn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
        getCodeBtn.tag = 999;
        [self addSubview:getCodeBtn];
        getCodeBtn.right = self.width - 30;
        [getCodeBtn addTarget:self action:@selector(getCodeAction:) forControlEvents:UIControlEventTouchUpInside];
        [getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        getCodeBtn.backgroundColor = BY_THEME_COLOR;
        getCodeBtn.titleLabel.font = FONT(14);
        getCodeBtn.layer.cornerRadius = 4.0;
        
    }

}
- (void)getCodeAction:(UIButton *)btn{
    if (self.clickCodeBlock) {
        self.clickCodeBlock(btn);
    }
}

#pragma mark -- textfieldDelegate
//-(void)textFieldDidBeginEditing:(UITextField *)textField{
//    if (self.editTextfieldBlock) {
//        self.editTextfieldBlock(textField);
//    }
//}

// 创建视图(文本输入框加图标)
- (void)_initView{
    // logo
    CGFloat x,y,width,height;
    x = (SCREEN_WIDTH - 100)/2;
    y = 20;
    width = height = 100;
    UIImageView *logoImg = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    logoImg.layer.cornerRadius = height/2;
    logoImg.backgroundColor = [UIColor redColor];
    [self addSubview:logoImg];
    
    
    //循环创建textfiled
    ByTextField *filed ;
    for (int i = 0; i<_titlesAry.count; i++) {
        CGFloat margin;//文本输入框之间的间距
        if (_type == ByUserViewLogin) {
            margin = 30;
        }else{
            margin = 15;
        }
        x = 20;
        y = CGRectGetMaxY(logoImg.frame) + 30 + LineHeight *i + margin*i;
        width = self.frame.size.width - x *2;
        filed = [[ByTextField alloc] initWithFrame:CGRectMake(x, y, width, LineHeight)];
        filed.borderStyle = UITextBorderStyleRoundedRect;
//        filed.backgroundColor = [UIColor clearColor];
        //若涉及输入密码
        if ([_titlesAry[i] containsString:@"密码"]) {
            filed.secureTextEntry = YES;
        }
        if ([_titlesAry[i] containsString:@"手机号"] || [_titlesAry[i] containsString:@"验证码"]) {
            filed.keyboardType = UIKeyboardTypeNumberPad;
        }
        if (_imgAry.count != 0) {
            UIImageView *leftImg = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, LineHeight - 10, LineHeight - 10)];
            [leftImg setImage:[UIImage imageNamed:_imgAry[i]]];
            UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LineHeight, LineHeight)];
            [leftView addSubview:leftImg];
            filed.leftView = leftView;
            filed.leftViewMode = UITextFieldViewModeAlways;
            
        }
        NSMutableAttributedString *paleceholderAttr = [[NSMutableAttributedString alloc] initWithString:_titlesAry[i]];
        [paleceholderAttr addAttribute:NSFontAttributeName value:FONT(14) range:NSMakeRange(0, paleceholderAttr.length)];
        [filed setAttributedPlaceholder:paleceholderAttr];
        filed.tag = 10+ i;
        [self addSubview:filed];
    }
    //若不是登录,第二栏都需要一个获得验证码的按钮
    if (_type != ByUserViewLogin) {
        filed = [self viewWithTag:11];
        width = filed.frame.size.width - 100;
        filed.size = CGSizeMake(width, LineHeight);
        width = 90;
        height = LineHeight - 10;
        y = CGRectGetMaxY(logoImg.frame) +30 +LineHeight +15 + 2;
        _captchaButton = [[ByLoginCaptchaButton alloc] initWithFrame:CGRectMake(0, y, width, height)];
        _captchaButton.right = self.width - 30;
        _captchaButton.tag = 99;

        [self addSubview:_captchaButton];
    }
}

#pragma mark - 密码是否显示
- (void)isShowOrNot:(UIButton *)button{
    button.selected = !button.selected;
    UITextField *textField = [self viewWithTag:button.tag - 100 + 10];
    textField.secureTextEntry = button.selected;
}


@end
